﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace TVBox源合并工具
{
    public partial class AddSourceForm : UIForm
    {
        public string name;
        public string path;
        public int type;
        public bool isLoadSource;

        #region 构造函数
        public AddSourceForm()
        {
            InitializeComponent();
        }
        public AddSourceForm(string name, string path, int type)
        {
            InitializeComponent();

            this.name = name;
            this.path = path;
            this.type = type;
        }
        #endregion

        #region 初始化
        private void UIAddSourceForm_Load(object sender, EventArgs e)
        {
            this.cmbPath.SelectedIndex = this.type;
            switch (this.type)
            {
                case 0:
                    this.btnSelect.Visible = false;
                    break;
                case 1:
                    this.btnSelect.Visible = true;
                    break;
            }

            this.txtName.Text = name;
            this.txtPath.Text = path;
        }
        #endregion

        #region 选择按钮
        private void btnSelect_Click(object sender, EventArgs e)
        {
            string file = "";
            if (Sunny.UI.FileEx.OpenDialog(ref file))
            {
                this.path = file;
                this.txtPath.Text = file;  
            }
        }
        #endregion

        #region 下拉列表选择
        private void cmbPath_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.type != this.cmbPath.SelectedIndex)
            {
                this.txtPath.Text = "";
                this.path = "";
            }

            this.type = this.cmbPath.SelectedIndex;

            switch (this.type)
            {
                case 0:
                    this.btnSelect.Visible = false;
                    break;
                case 1:
                    this.btnSelect.Visible = true;
                    break;
            }
        }
        #endregion

        #region 确定按钮
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                ShowWarningTip("请输入名称");
                return;
            }

            if (this.type == 0 && string.IsNullOrEmpty(this.txtPath.Text))
            {
                ShowWarningTip("请输入网址");
                return;
            }
            else if (this.type == 1 && string.IsNullOrEmpty(this.txtPath.Text))
            {
                ShowWarningTip("请选择文件");
                return;
            }

            this.name = this.txtName.Text;
            this.path = this.txtPath.Text;

            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 取消按钮
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        #endregion
    }
}
